package org.wattdepot.cli;

import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import org.wattdepot.client.ResourceNotFoundException;
import org.wattdepot.client.WattDepotClientException;
import org.wattdepot.resource.sensordata.jaxb.SensorData;
import org.wattdepot.util.tstamp.Tstamp;

/**
 * Executes the dailysensordata command.
 * 
 * @author Scott Wong and Kelli Sawai
 * 
 */
public class DaySensorData extends CommandLineInterface implements WattDepotCliCommand {

  /**
   * Executes and returns the results of the dailysensordata command.
   * 
   * @param args The arguments required to execute this method.
   * @return The results of the command.
   */
  public String doCommand(String args) {
    try {
      if (args.matches("dailysensordata \\w* day \\S*")) {
        String[] sensorInput = args.split(" ");
        String sourceName = sensorInput[1].toUpperCase(Locale.ENGLISH);
        String day = sensorInput[3];
        XMLGregorianCalendar startDay = toTimestamp(day);
        // Check if the timestamp is in the future.
        XMLGregorianCalendar now =
            DatatypeFactory.newInstance()
                .newXMLGregorianCalendar(new GregorianCalendar());
        if (startDay.compare(now) >= 0) {
          return futureTimestampMessage;
        }
        XMLGregorianCalendar endDay = Tstamp.incrementDays(startDay, 1);
        return listSensorDataDay(sourceName, startDay, endDay);
      }
      else {
        return "The command you have entered is invalid.";
      }
    }
    catch (WattDepotClientException e) {
      return "An error occured while executing the requested command.";
    }
    catch (Exception e) {
      return "An error occured while executing the requested command.";
    }
  }

  /**
   * Prints a table listing all of the sensor data instances for the specified day.
   * 
   * @param source the plant name.
   * @param startDay the start of the requested date.
   * @param endDay the end of the requested date
   * @return A table of all the sensor data for an inputed day.
   * @throws WattDepotClientException if an error occurs.
   */
  private String listSensorDataDay(String source, XMLGregorianCalendar startDay,
      XMLGregorianCalendar endDay) throws WattDepotClientException {

    StringBuffer buffer = new StringBuffer(50);
    try {
      List<SensorData> dataList = client.getSensorDatas(source, startDay, endDay);
      for (int i = 0; i < dataList.size(); i++) {
        SensorData data = dataList.get(i);
        buffer.append(data.getTimestamp());
        buffer.append(" Tool: ").append(data.getTool());
        buffer.append(" Properties: ").append(formatProperties(data.getProperties()));
        buffer.append(nl);
      }
    }
    catch (ResourceNotFoundException e) {
      return resourceMessage;
    }

    return buffer.toString();
  }
}
